﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.UI;
using System.Web;

public partial class Controls_PreviewLoanOrders : System.Web.UI.UserControl
{
    Unit mWidth = 350;
    Unit mHeight = 150;
    Unit mSelectButtonWidth = 100;
    string mTitleWindow = "Visualizarea Graficului de achitari";
    string mSelectButtonText = "Grafic achitari";
    string appPath = string.Empty;

    int parameterX = 0;
    int parameterY = 0;

    public Unit Width
    {
        get { return mWidth; }
        set { mWidth = value; }
    }

    public Unit Height
    {
        get { return mHeight; }
        set { mHeight = value; }
    }

    public Unit SelectButtonWidth
    {
        get { return mSelectButtonWidth; }
        set { mSelectButtonWidth = value; }
    }

    public string TitleWindow
    {
        get { return mTitleWindow; }
        set { mTitleWindow = value; }
    }

    public string SelectButtonText
    {
        get { return mSelectButtonText; }
        set { mSelectButtonText = value; }
    }

    public int ParameterX
    {
        set { parameterX = value; }
    }

    public int ParameterY
    {
        set { parameterY = value; }
    }


    public DataTable DataSource
    {
        get
        {
            DataTable dt = null;
            if (Session[programmaticPopup.ClientID] != null)
                dt = (DataTable)Session[programmaticPopup.ClientID];
            return dt;
        }
        set { Session[programmaticPopup.ClientID] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (parameterX != 0) modalPopup.X = parameterX;
        if (parameterY != 0) modalPopup.Y = parameterY;

        appPath = Utils.GetApplicationPath(Page.Request);

        WindowTitleLabel.Text = mTitleWindow;
        printLoanButton.OnClientClick = "javascript:CallPrint('" + PreviewCreditForm.ClientID + "');";
    }

    public void BindDataSource()
    {
        DataTable mSourceTable = new DataTable();
        if (Session[programmaticPopup.ClientID] != null)
            mSourceTable = (DataTable)Session[programmaticPopup.ClientID];

        previewLoanGridView.DataSource = mSourceTable;
        previewLoanGridView.DataBind();       
    }

    public void Show()
    {
        modalPopup.Show();
    }

    decimal sumPercentForFooter = 0;
    decimal sumRateForFooter = 0;
    decimal sumAmountForFooter = 0;

    decimal sumPercentForFooter_Rated = 0;
    decimal sumRateForFooter_Rated = 0;
    decimal sumAmountForFooter_Rated = 0;

    protected void previewLoanGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label currentPercentLabel = (Label)e.Row.FindControl("percentAmountLabel");
            if (currentPercentLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(currentPercentLabel.Text, out tempVal);
                sumPercentForFooter += tempVal;
            }

            Label rateAmountLabel = (Label)e.Row.FindControl("rateAmountLabel");
            if (rateAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(rateAmountLabel.Text, out tempVal);
                sumRateForFooter += tempVal;
            }

            Label totalAmountLabel = (Label)e.Row.FindControl("totalAmountLabel");
            if (totalAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(totalAmountLabel.Text, out tempVal);
                sumAmountForFooter += tempVal;
            }

            ///////
            Label currentPercent_RatedLabel = (Label)e.Row.FindControl("percentAmount_RatedLabel");
            if (currentPercent_RatedLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(currentPercent_RatedLabel.Text, out tempVal);
                sumPercentForFooter_Rated += tempVal;
            }

            Label rateAmount_RatedLabel = (Label)e.Row.FindControl("rateAmount_RatedLabel");
            if (rateAmount_RatedLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(rateAmount_RatedLabel.Text, out tempVal);
                sumRateForFooter_Rated += tempVal;
            }

            Label totalAmount_RatedLabel = (Label)e.Row.FindControl("totalAmount_RatedLabel");
            if (totalAmount_RatedLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(totalAmount_RatedLabel.Text, out tempVal);
                sumAmountForFooter_Rated += tempVal;
            }
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label totalPercentAmountLabel = (Label)e.Row.FindControl("totalPercentAmountLabel");
            totalPercentAmountLabel.Text = sumPercentForFooter.ToString();

            Label totalRateAmountLabel = (Label)e.Row.FindControl("totalRateAmountLabel");
            totalRateAmountLabel.Text = sumRateForFooter.ToString();

            Label totalTotalAmountLabel = (Label)e.Row.FindControl("totalTotalAmountLabel");
            totalTotalAmountLabel.Text = sumAmountForFooter.ToString();

            Label totalPercentAmount_RatedLabel = (Label)e.Row.FindControl("totalPercentAmount_RatedLabel");
            totalPercentAmount_RatedLabel.Text = sumPercentForFooter_Rated.ToString();

            Label totalRateAmount_RatedLabel = (Label)e.Row.FindControl("totalRateAmount_RatedLabel");
            totalRateAmount_RatedLabel.Text = sumRateForFooter_Rated.ToString();

            Label totalTotalAmount_RatedLabel = (Label)e.Row.FindControl("totalTotalAmount_RatedLabel");
            totalTotalAmount_RatedLabel.Text = sumAmountForFooter_Rated.ToString();
        }
    }

}